package in.omerjerk.fullscreenimmersive;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import de.hdodenhof.circleimageview.CircleImageView;
import java.util.Calendar;

public class ImmersiveService extends Service {
    public static final int FLAG_HIDE_NAV_BAR = 2;
    public static final int FLAG_HIDE_NONE = 1;
    public static final int FLAG_HIDE_STATUS_BAR = 4;
    public static final String KEY_MODE = "mode";
    private static final String TAG = "ImmersiveService";
    /* access modifiers changed from: private */
    public CircleImageView keyBoard;
    /* access modifiers changed from: private */
    public int mode = 0;
    /* access modifiers changed from: private */
    public ImageView phImageView;
    /* access modifiers changed from: private */
    public WindowManager windowManager;

    private int getImmersiveParams(boolean z, boolean z2) {
        int i = z ? 1028 : 0;
        return z2 ? i | 4610 : i;
    }

    @Nullable
    public IBinder onBind(Intent intent) {
        return null;
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        WindowManager.LayoutParams layoutParams;
        Log.d(TAG, "onStartCommand called");
        if (intent != null) {
            this.mode = intent.getIntExtra(KEY_MODE, 0);
            Log.e(TAG, "mode = " + this.mode);
        } else {
            this.mode = 0;
        }
        this.windowManager = (WindowManager) getSystemService("window");
        if (this.keyBoard == null) {
            this.keyBoard = (CircleImageView) LayoutInflater.from(this).inflate(R.layout.view_keyboard, (ViewGroup) null);
            this.keyBoard.setImageResource(R.drawable.keyboard);
            if (Build.VERSION.SDK_INT < 26) {
                layoutParams = new WindowManager.LayoutParams(170, 170, 2010, 262152, -3);
            } else {
                Log.e("omerjerk", "else");
                layoutParams = new WindowManager.LayoutParams(170, 170, 2038, 262152, -3);
            }
            layoutParams.gravity = 51;
            layoutParams.x = 0;
            layoutParams.y = 300;
            this.windowManager.addView(this.keyBoard, layoutParams);
            initViewEvents(layoutParams);
        }
        if (this.phImageView == null) {
            this.phImageView = new ImageView(this);
            this.phImageView.setImageResource(0);
            this.windowManager.addView(this.phImageView, getLayoutParams());
        }
        this.keyBoard.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ImmersiveService.this.tempHideNone();
            }
        });
        this.phImageView.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                return false;
            }
        });
        this.phImageView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            public void onSystemUiVisibilityChange(int i) {
                Log.e(ImmersiveService.TAG, "Visibility state = " + i);
            }
        });
        updateState(this.mode);
        return 2;
    }

    private WindowManager.LayoutParams getLayoutParams() {
        WindowManager.LayoutParams layoutParams;
        if (Build.VERSION.SDK_INT < 26) {
            layoutParams = new WindowManager.LayoutParams(140, 140, 2010, 131088, -3);
        } else {
            layoutParams = new WindowManager.LayoutParams(140, 140, 2038, 131088, -3);
        }
        layoutParams.gravity = 51;
        layoutParams.x = 0;
        layoutParams.y = 100;
        return layoutParams;
    }

    public void onDestroy() {
        updateState(0);
        if (this.windowManager != null) {
            if (!(this.phImageView == null || this.phImageView.getWindowToken() == null)) {
                this.windowManager.removeViewImmediate(this.phImageView);
            }
            if (!(this.keyBoard == null || this.keyBoard.getWindowToken() == null)) {
                this.windowManager.removeViewImmediate(this.keyBoard);
            }
        }
        super.onDestroy();
    }

    /* access modifiers changed from: private */
    public void updateState(int i) {
        int i2;
        if ((i & 1) != 0) {
            i2 = getImmersiveParams(false, false);
            this.phImageView.setVisibility(8);
        } else {
            int i3 = i & 2;
            if (i3 == 0 || (i & 4) != 0) {
                if (((i & 4) != 0) && (i3 != 0)) {
                    i2 = getImmersiveParams(true, true);
                    this.phImageView.setVisibility(0);
                } else {
                    i2 = getImmersiveParams(false, false);
                    this.phImageView.setVisibility(8);
                }
            } else {
                i2 = getImmersiveParams(false, true);
                this.phImageView.setVisibility(0);
            }
        }
        this.phImageView.setSystemUiVisibility(i2);
    }

    /* access modifiers changed from: private */
    public void tempHideNone() {
        updateState(1);
        this.phImageView.setVisibility(8);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                ImmersiveService.this.updateState(ImmersiveService.this.mode);
                ImmersiveService.this.phImageView.setVisibility(0);
            }
        }, 3000);
    }

    private void initViewEvents(final WindowManager.LayoutParams layoutParams) {
        this.keyBoard.setOnTouchListener(new View.OnTouchListener() {
            private static final int MAX_CLICK_DURATION = 200;
            private float initialTouchX;
            private float initialTouchY;
            private int initialX;
            private int initialY;
            private long startClickTime;

            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case 0:
                        this.initialX = layoutParams.x;
                        this.initialY = layoutParams.y;
                        this.initialTouchX = motionEvent.getRawX();
                        this.initialTouchY = motionEvent.getRawY();
                        this.startClickTime = Calendar.getInstance().getTimeInMillis();
                        return true;
                    case 1:
                        if (Calendar.getInstance().getTimeInMillis() - this.startClickTime < 200) {
                            ImmersiveService.this.keyBoard.callOnClick();
                        }
                        return true;
                    case 2:
                        layoutParams.x = this.initialX + ((int) (motionEvent.getRawX() - this.initialTouchX));
                        layoutParams.y = this.initialY + ((int) (motionEvent.getRawY() - this.initialTouchY));
                        ImmersiveService.this.windowManager.updateViewLayout(ImmersiveService.this.keyBoard, layoutParams);
                        return true;
                    default:
                        return false;
                }
            }
        });
    }
}
